{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 格式化输出"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## % 方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name:june, age:20\n"
     ]
    }
   ],
   "source": [
    "# 格式化符号（占位）： %s, %d, %f, %x\n",
    "# %s: 输出字符串\n",
    "# %d: 输出int\n",
    "# %f: 输出float\n",
    "# %x: 输出16进制数据\n",
    "\n",
    "print('name:%s, age:%d' % ('june',20))\n",
    "# s:str, d:int，f：float"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name:june, age:20.23\n"
     ]
    }
   ],
   "source": [
    "age=20.229943\n",
    "print('name:%s, age:%0.2f' % ('june',age))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ff\n"
     ]
    }
   ],
   "source": [
    "print('%x' % (255))  # format 16进制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-10T03:38:33.222498Z",
     "start_time": "2022-03-10T03:38:26.040598Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "what's your name?小张\n",
      "Hey, 小张!~  Nice to meet you!~\n"
     ]
    }
   ],
   "source": [
    "name = input(\"what's your name?\")\n",
    "print('Hey, %s!~  Nice to meet you!~' % name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## str.format 方式\n",
    "\n",
    "- str.format,  用{}占位, 通过 {} 和 `.format` 来代替以前的 %\n",
    "- format 函数可以接受不限个参数，位置可以不按顺序。\n",
    "    - 不设置指定位置，按默认顺序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hune is heij 40\n"
     ]
    }
   ],
   "source": [
    "print('{} is heij {}'.format('hune',40))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'world hello'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"{1} {0}\".format(\"hello\", \"world\")  # 设置指定位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'world hello world'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"{1} {0} {1}\".format(\"hello\", \"world\")  # 设置指定位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'M0：7万亿, M1:60万亿,M2 190万亿'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 设定参数\n",
    "\n",
    "\"M0：{m0}万亿, M1:{m1}万亿,M2 {m2}万亿\".format(m0=7, m2=190,m1=60)  # 如果没有用这种形式，则需要使用位置指代"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-10T03:42:39.499388Z",
     "start_time": "2022-03-10T03:42:39.479441Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "M0:7,M1:60,M2:190\n",
      "M0:7,M1:60,M2:190\n"
     ]
    }
   ],
   "source": [
    "# 传入dictionary\n",
    "m2={'m0':7,'m1':60,'m2':190}\n",
    "print('M0:{m[m0]},M1:{m[m1]},M2:{m[m2]}'.format(m=m2))\n",
    "print('M0:{0[m0]},M1:{0[m1]},M2:{0[m2]}'.format(m2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "M0:190,M1:60,M2:7\n"
     ]
    }
   ],
   "source": [
    "# 传入list\n",
    "m=[7,60,190]\n",
    "print('M0:{0[2]},M1:{0[1]},M2:{0[0]}'.format(m))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'abc:good,{0}'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用大括号 {} 来转义大括号\n",
    "'abc:{},{{0}}'.format('good')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 格式化数字的多种方法\n",
    "\n",
    "参考: https://www.runoob.com/python/att-string-format.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'344.440%'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'{:.3%}'.format(3.4444)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
